﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DTO;
using BUS;
using Telerik.Web.UI;
namespace TCV_WEB.Admin.Page
{
    public partial class Status : System.Web.UI.Page
    {
        string strActivity = "";
        string strProductTypeID = "";
        string strLanguageID = "EN";
        public string Username;
        string sErr = "";
        public static int ContentLength;
        // ProductType pro;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UsernameAdmin"] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            else
            {
                Username = Session["UsernameAdmin"].ToString();
            }
            if (!Page.IsPostBack)
            {
                // pro = new ProductType(strLanguageID);
                this.Reset();
                this.LoadDataGrid();
                //Session["Sort"] = "ASC";
                //  LoadComboxBox();
                //this.lblManageTypeProduct.Text = CommonTask.GetFormTitle("ManageProductTitle");
            }
            btnSave.Attributes.Add("OnClick", "return validform()");
            btnDelete.Attributes.Add("OnClick", "return Confirm_Delete()");
        }


        private void LoadDataGrid()
        {
            DataTable dtb = new DataTable();
            try
            {
                StatusControl cateControl = new StatusControl();
                dtb = cateControl.GetAll(ref sErr);
                dgvList.DataSource = dtb;
                //dgvList.DataBind();               
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
            finally
            {
                dtb.Dispose();
            }
        }



        private void LoadData(string strProductID)
        {
            //strUploadFolder = "~/" + ConfigurationManager.AppSettings["UploadFolder"].ToString();
            int ID = Convert.ToInt32(strProductID);
            StatusControl cateControl = new StatusControl();
            StatusInfo cate = cateControl.Get(ID, ref sErr);
            hdID.Value = cate.ID.ToString();
            this.txtName.Text = cate.Name;
            txtNameVN.Text = cate.NameVN;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //  pro = new ProductType(strLanguageID);
            if (txtName.Text == "")
            {
                lblErr.Text = "Category's name is not NULL";
                return;
            }
            StatusInfo cate = new StatusInfo();
            try
            {
                if (this.hdID.Value != "")
                {
                    strActivity = "Update";
                    cate.ID = Convert.ToInt32(hdID.Value);
                }
                else
                {
                    strActivity = "Insert";
                    //pro.ProductTypeID = "";

                }
                cate.Code = txtCode.Text;
                cate.Name = this.txtName.Text;
                cate.NameVN = txtNameVN.Text;


                StatusControl cateControl = new StatusControl();
                if (strActivity == "Update")
                {
                    sErr = cateControl.Update(cate);
                }
                else if (strActivity == "Insert")
                {
                    cateControl.Add(cate, ref sErr);
                }
                if (sErr == "")
                {
                    lblErr.Text = "Thành công";
                    this.Reset();
                }
                else
                {
                    lblErr.Text = sErr;
                }
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message.ToString();
            }
            dgvList.Rebind();

        }

        public void Reset()
        {
            this.txtName.Text = "";
            this.lblErr.Text = "";
            this.hdID.Value = "";
            txtCode.Text = "";
            txtNameVN.Text = "";

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            // pro = new ProductType(strLanguageID);
            try
            {
                string strID = "";
                for (int i = 0; i < dgvList.SelectedItems.Count; i++)
                {

                    strID += dgvList.MasterTableView.DataKeyValues[dgvList.SelectedItems[i].ItemIndex]["ID"].ToString() + ",";

                }
                StatusControl cateControl = new StatusControl();
                sErr = cateControl.Delete_Status(strID);
                dgvList.Rebind();
                if (sErr == "")
                    lblErr.Text = "Xóa thành công";
                else
                    lblErr.Text = "Lỗi: " + sErr;
                this.Reset();
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            this.Reset();
        }

        protected void dgvList_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            LoadDataGrid();
        }
        

        protected void dgvList_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "LOAD")
            {
                strProductTypeID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim();
                e.Item.Selected = true;
                LoadData(strProductTypeID);
            }
        }

        protected void dgvList_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.DataItem != null)
            {
                DataRow row = ((DataRowView)e.Item.DataItem).Row;
                Label sttCtrl = (Label)e.Item.FindControl("colSTT");
                sttCtrl.Text = (e.Item.ItemIndex + 1).ToString("00");                
            }
        }

    }
}
